package tech.cyhk.cloud.demo.product.controller;

import java.math.BigDecimal;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.cyhk.cloud.demo.product.bean.Product;

/**
 * @author swd
 * @date 2025/1/20
 */
@Slf4j
@RequestMapping("/product")
@RestController
public class ProductController {

    @GetMapping("hello")
    public String hello(HttpServletRequest request, HttpServletResponse response) {
        String xToken = request.getHeader("x-token");
        log.info("method hello start.. token={}", xToken);
        if (true) {
//            throw new RuntimeException("product error");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        response.addHeader("x-token-res", xToken.toUpperCase(Locale.ROOT));
        return "hello, product";
    }

    @GetMapping("/{id}")
    public Product getProductById(@PathVariable Long id) {

        Product product = new Product();
        product.setId(id);
        product.setPrice(new BigDecimal("7.66"));
        product.setName("haha-" + id);
        product.setNum(2);
        return product;
    }
}
